package com.xu.threadLocal;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 *
 * @author xudenghui
 * @create 2023-03-08 10:53
 **/
public class ThreadLocalDemo {
    private static final ThreadLocal<SimpleDateFormat> t1 = new ThreadLocal<SimpleDateFormat>();

   static class parseDate implements  Runnable{
         int i = 0;

        public parseDate(int i) {
            this.i = i;
        }

        @Override
        public void run() {
            try {
                if (t1.get() == null){
                    t1.set(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
                }
                Date t = t1.get().parse("2023-03-08 19:29:"+i%60);
                System.out.println(i+":"+t);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }


    public static void main(String[] args) {
        ExecutorService service = Executors.newFixedThreadPool(10);
        for (int i = 0; i < 1000; i++) {
            service.execute(new parseDate(i));
        }
    }
}


